using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Logger;

namespace FiltronASP.Models.DAO
{
    /// <summary>
    /// Klasa dla operacji bazodanowych specyficznych dla tabeli <code>user_page_types</code>.
    /// </summary>
    public class UserPageTypeDAO : Abstracts.AbstractGenericDAO<UserPageType, System.Nullable<int>>,
        Interfaces.IUserPageTypeDAO
    {
        /// <summary>
        /// Metoda zwracajaca nazwe parametru bedacego kluczem glownym tabeli.
        /// </summary>
        /// <returns>nazwa klucza glownego</returns>
        protected override string GetKeyProperty()
        {
            return "user_page_type_id";
        }

        public IQueryable<UserPageType> FindByUserId(int? userId)
        {
            if (!userId.HasValue)
            {
                return null;
            }   //if
            if (userId <= 0)
            {
                return null;
            }   //if
            try
            {
                IQueryable<UserPageType> userPageTypes = this._cmsEntitiesDataContext.UserPageTypes.Where(
                    upt => upt.user_id.Equals(userId.Value)
                );
                if (!(userPageTypes.Any()))
                {
                    return null;
                }   //if
                return userPageTypes;
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        public UserPageType FindByUserIdAndPageTypeId(int? userId, int? pageTypeId)
        {
            if (!userId.HasValue)
            {
                return null;
            }   //if
            if (userId <= 0)
            {
                return null;
            }   //if
            if (!pageTypeId.HasValue)
            {
                return null;
            }   //if
            if (pageTypeId <= 0)
            {
                return null;
            }   //if
            try
            {
                return this._cmsEntitiesDataContext.UserPageTypes.Where(
                    upt => upt.user_id.Equals(userId.Value)
                        && upt.page_type_id.Equals(pageTypeId.Value)
                ).SingleOrDefault();
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return null;
        }

        /// <summary>
        /// Usuwanie obiektów na podstawie id uzytkownika.
        /// </summary>
        /// <param name="userId">id uzytkownika</param>
        /// <returns>true w przypadku usunięcia, false w przypadku bledu</returns>
        public bool DeleteByUserId(int? userId)
        {
            if (!userId.HasValue)
            {
                return true;
            }   //if
            if (userId <= 0)
            {
                return true;
            }   //if
            try
            {
                IQueryable<UserPageType> userPageTypes = this._cmsEntitiesDataContext.UserPageTypes.Where(
                    upt => upt.user_id.Equals(userId.Value)
                );
                if (!(userPageTypes.Any()))
                {
                    return true;
                }
                foreach (UserPageType userPageType in userPageTypes)
                {
                    this._cmsEntitiesDataContext.UserPageTypes.DeleteOnSubmit(userPageType);
                }
                try
                {
                    this._cmsEntitiesDataContext.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                //if
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }

        /// <summary>
        /// Usuwanie obiektów na podstawie id grupy stron.
        /// </summary>
        /// <param name="userId">id grupy stron</param>
        /// <returns>true w przypadku usunięcia, false w przypadku bledu</returns>
        public bool DeleteByPageTypeId(int? pageTypeId)
        {
            if (!pageTypeId.HasValue)
            {
                return true;
            }   //if
            if (pageTypeId <= 0)
            {
                return true;
            }   //if
            try
            {
                IQueryable<UserPageType> userPageTypes = this._cmsEntitiesDataContext.UserPageTypes.Where(
                    upt => upt.page_type_id.Equals(pageTypeId.Value)
                );
                if (!(userPageTypes.Any()))
                {
                    return true;
                }
                foreach (UserPageType userPageType in userPageTypes)
                {
                    this._cmsEntitiesDataContext.UserPageTypes.DeleteOnSubmit(userPageType);
                }
                try
                {
                    this._cmsEntitiesDataContext.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                //if
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }

        /// <summary>
        /// Usuwanie obiektów na podstawie id wersji jezykowej.
        /// </summary>
        /// <param name="languageId">id wersji jezykowej</param>
        /// <returns>true w przypadku usunięcia, false w przypadku bledu</returns>
        public bool DeleteByLanguageId(int? languageId)
        {
            if (!languageId.HasValue)
            {
                return true;
            }   //if
            if (languageId <= 0)
            {
                return true;
            }   //if
            try
            {
                IQueryable<UserPageType> userPageTypes = this._cmsEntitiesDataContext.UserPageTypes.Where(
                    upt => (this._cmsEntitiesDataContext.PageTypes.Where(
                        pt => pt.language_id.Equals(languageId)).Select(pt => pt.page_type_id).
                        Contains(upt.page_type_id))
                );
                if (!(userPageTypes.Any()))
                {
                    return true;
                }
                foreach (UserPageType userPageType in userPageTypes)
                {
                    this._cmsEntitiesDataContext.UserPageTypes.DeleteOnSubmit(userPageType);
                }
                try
                {
                    this._cmsEntitiesDataContext.SubmitChanges();
                    return true;
                }
                catch (Exception ex)
                {
                    MyLogger.Log(ex);
                }   //catch
                //if
            }   //try
            catch (Exception ex)
            {
                MyLogger.Log(ex);
            }   //catch
            return false;
        }

    }
}
